package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.dto.DeleteOrderRequest;
import com.cskaoyan.order.dto.DeleteOrderResponse;
import com.cskaoyan.order.service.OrderCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 类说明
 *
 * @author zhangfuqiang
 * @date 2022/5/20
 */
@RestController
public class ShoppingOrderDeleteController {
    @Autowired
    OrderCoreService orderCoreService;

    //http://localhost:8103/shopping/order/19081913521928018
    @DeleteMapping("/shopping/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String id, HttpServletRequest request) {
        // 获取Token中的用户信息
        Long uid = null;
        try {
            String user_info = request.getHeader("user_info");
            JSONObject jsonObject = JSON.parseObject(user_info);
            uid = Long.parseLong(jsonObject.get("uid").toString());
        } catch (Exception e) {
            return new ResponseUtil().setErrorMsg("用户未登录");
        }
        // 获得商品id
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(id);
        DeleteOrderResponse response = orderCoreService.deleteOrder(deleteOrderRequest);
        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
